// pages/center/center.js
const {denglu,getOpenId,vipgrade,gold,scan,newname,activation,getcode,getlogin} =require('../../util/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codenum:'',
    unionId:'',
    count_down:60,
    token:"",
   putname:"",
    imgUrls:"",
    imglist:"",
    phone:"",
    memberId:"",
    nickName:"",
    avatarUrl:"",
    encryptedData:"",
    sessionKey:'',
    iv:"",
    code:"",
    openId:'',
    viplist:"",
    goldCoin:"",
    abc:false,
    shows: false, 
    onshow:null,
    icon:'',
    webviewUrl:'',
    iSselect:false,
    win:false,
    get_code_btn:true
  },
  checkboxChange(e){
this.setData({
  iSselect:!this.data.iSselect
})
  },

  zuyongjilu(){
    wx.navigateTo({
      url: '/pages/jilu/jilu',
    })
  },
  makePhoneCall(){
    wx.makePhoneCall({
      // 拨打号码
      phoneNumber: '4006115859' 
    })
  },
  jump2:function(){
    wx.navigateTo({
     url: "/pages/manage/manage",
     success: function(res) {
     },
     fail: function(res) {
     },
     complete: function(res) {
     },
    })
   },
   jump3:function(){
    wx.navigateTo({
     url: "../center/jineng/jineng",
     success: function(res) {
     },
     fail: function(res) {
     },
     complete: function(res) {
     },
    })
   },
   
    myadress(){
      wx.navigateTo({
        url: '/pages/myadress/myadress',
      })
    },

//输入手机号
  inputphone(e){
    this.setData({
      phone:e.detail.value
    })
  },

  //点击登录
  getlogin(){
    if(!this.data.iSselect){
      wx.showToast({
        title: '请先阅读用户协议',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.phone.length!=11){
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.codenum.length!=6){
      wx.showToast({
        title: '请输入正确验证码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.login({
      success:(res)=>{
        this.setData({
          code:res.code
        })
      }
    })  
    let p={
      phone:this.data.phone,
      code:this.data.codenum,
     wxcode:this.data.code
    }
    getlogin(p).then(res=>{
        if(res.code==200){
          console.log(res)
          this.setData({
              onshow:false,
              win:false,
             phone:res.aaMember.memberPhone,
             nickName:res.aaMember.nickName,
             imgUrls:res.aaMember.icon
            })
              wx.setStorageSync('nickName', this.data.nickName)
              wx.setStorageSync('phone',this.data.phone)
              wx.setStorageSync('memberId', res.aaMember.id)
              wx.setStorageSync('onshow', false)
              wx.setStorageSync('win', false)
              wx.setStorageSync('imgUrls', this.data.imgUrls)
               wx.setStorageSync('token', res.token)
               wx.setStorageSync('memberName', res.aaMember.memberName)
               wx.setStorageSync('authentication', res.aaMember.authentication)
               if(wx.getStorageSync('tuiguangId')!=''){
                 let p={
                   memberId:res.aaMember.id,
                   userId:wx.getStorageSync('tuiguangId')
                 }
                 scan(p).then(res=>{
                     if(res.code==200){
                       console.log("绑定成功")
                     }
                 })
                 this.onShow()
               }
               } else{
                 console.log(1111111111)
                 wx.showToast({
                   title:res.msg,
                   icon: 'none',
                   duration: 2000
                 })
               }
             })   
  },
  handcode(){
    if(this.data.phone.length<11){
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let p={
      phoneNumber:this.data.phone
    }
    getcode(p).then(res=>{
      console.log(res)
      if(res.code==200){
        
        this.get_code_settime();
        this.setData({
          get_code_btn:false
        })
        wx.showToast({
          title: '已发送',
          icon: 'none',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  phonecode(e){
    this.setData({
      codenum:e.detail.value
    })
  },

         lookxieyi(){
           wx.navigateTo({
             url: '/pages/xieyi/xieyi',
           })
         },
  jinbi(){
    wx.navigateTo({
      url: '/pages/center/king/king',
    })
  }
  ,
  vip(){
    // that.setData({
    //   onshow:true
    // })
    wx.navigateTo({
      url: '/pages/dengji/dengji',
    })
  }
  ,
  zuyongdingdan(){
    wx.navigateTo({
      url: '/pages/lister/lister',
    })
  },
  activation(e){
    if(wx.getStorageSync('authentication')==0){
      wx.navigateTo({
        url: '/pages/renzheng/renzheng',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/eqjihuo/eqjihuo',
    })
    

  },
   //验证码按钮倒计时方法
   get_code_settime: function(that) {
    var that = this;
    var countDown = that.data.count_down;    //获取倒计时初始值
    //如果定时器设置在外边，那么用户就看不到countDown的数值变化，所以要把定时器存进data里边
    that.setData({
        //这里把setInterval复制给变量名timer
        timer:setInterval(function(){
            //每隔一秒减1
            countDown--
            that.setData({
                get_code_btn:false,    //此处用于隐藏验证码点击按钮，使倒计时显示
                count_down:countDown   //倒计时计数 
            })
            //当计数为0时，清空定时器，使验证码按钮再次显示
            if(countDown == 0){
                clearInterval(that.data.timer);//此处我清除timer之后，count_down没有变回我原先设定的120，get_code_btn也没有变回默认的true，咱没搞明白，所以我又重新赋值了
                that.setData({
                    get_code_btn: true,
                    count_down: '60'
                })
            }
        },1000)
    })
},

  shezhi(){
    if(wx.getStorageSync('authentication')==0){
      wx.navigateTo({
        url: '/pages/renzheng/renzheng',
      })
    }else{
      wx.showToast({
        title: '已认证',
        icon:'success'
      })
    }
  },

  quxiao(){
    this.setData({
      abc:false
    })
  },
  manage(){
    wx.navigateTo({
      url: '/pages/manage/manage',
    })
  },
  renwu(){
    wx.navigateTo({
      url: '/pages/renwu/renwu',
    })
  },
 

  need(){
    wx.navigateTo({
      url: '/pages/bianmin/need/need',
    })
  },
  changgeimg(){
    let that=this
    
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
       
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: 'https://anjingbao.anjingbao.net/prod-api/api/common/appUpload',
          success (res){
            console.log(res)
            let jsonS = JSON.parse(res.data)
            console.log(jsonS)
            that.setData({
              imgUrls:jsonS.fileName 
            })
            let p={
              memberId:wx.getStorageSync('memberId'),
              nickName:wx.getStorageSync('nickName'),
              avatarUrl:jsonS.fileName
            }
            newname(p).then(res=>{
              console.log(res)
            })
            wx.setStorageSync('imgUrls', that.data.imgUrls)
          }
        })
      }

  })},
  handttp(){
   let name=this.data.nickName
    wx.navigateTo({
      url: '/pages/nicheng/nicheng?name='+name,
    })
  //  this.setData({

  //    xiugai:true
  //  })
  },

  name(e){
    this.setData({
      putname:e.detail.value
    })
    
  },
  yesname(){

    if(this.data.putname.length>0){
      this.setData({
        nickName:this.data.putname,
        xiugai:false
      })
      
      
      let p={
        memberId:wx.getStorageSync('memberId'),
        nickName:this.data.nickName,
        avatarUrl:this.data.imgUrls
      }
      console.log(p)
      newname(p).then(res=>{
        wx.setStorageSync('nickName', this.data.nickName)
        console.log(res)
      })

    }
    else{
      wx.showToast({
        title: '请输入昵称',
        icon: 'error',
        duration: 2000
      })
    }
   
  },
  xufeijilu(){
    wx.navigateTo({
      url: '/pages/xuzujilu/xuzujilu',
    })
  },
  noname(){
    this.setData({
      xiugai:false
    })
   },
   //点击遮罩层显示弹窗
   showwin(){
    this.setData({
      win:true  
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(){
   
    //如果缓存里的token为空，就显示遮罩层，并获取code，获取openid

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
   
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.getStorageSync('token')==""||wx.getStorageSync('token')==null){
      wx.setStorageSync('onshow', true)
    wx.login({
      success:(res)=>{
        console.log(res,'获取code')
        this.setData({
          code:res.code
        })
      }
    })  
  }
    this.setData({
      phone:wx.getStorageSync('phone'),
      imgUrls:wx.getStorageSync('imgUrls'),
    onshow:wx.getStorageSync('onshow'),
    win:wx.getStorageSync('win'),
      nickName:wx.getStorageSync('nickName')
    })
   
    let p={
      memberId:wx.getStorageSync('memberId')
    }
    vipgrade(p).then(res=>{
      console.log(res)
      this.setData({
        viplist:res.data.integralGrade
      })
    }
      )
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.setStorageSync('win', false)
    this.setData({
      codenum:'',
      win:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setStorageSync('win', false)
    this.setData({
      codenum:'',
      win:false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxianginfo.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})